package BLL;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * 检查数据库
 * @ClassName establishDatabase
 * @Description: TODO
 * @Author YELIUHUISHI
 * @Date 2020/2/28
 * @Version V1.0
 **/


public class establishDatabase {

    private Statement statement=null;
    //初始化connection
    public establishDatabase() {
        String path="src/configuration/sequential_query.properties";
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            Properties properties = new Properties();
            properties.load(fileInputStream);
            Connection connection = DriverManager.getConnection(properties.getProperty("mysqlURL"), properties.getProperty("mysqlUser"), properties.getProperty("mysqlPwd"));
            statement=connection.createStatement();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //判断数据库是否存在
    public boolean checkDatabase(){

        boolean flag=false;
        String chooseDatabase="show databases like 'sequential_query'";//判断数据库是否存在
        String createDatabase="create database sequential_query";//创建数据库

        try {
            ResultSet resultSet = statement.executeQuery(chooseDatabase);
            if(resultSet.next()){
                flag=true;
            }else{
                statement.executeUpdate(createDatabase);
                flag=checkDatabase();

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return flag;
    }

}
